package de.tudarmstadt.ukp.muip.fibqdojo.visitor;

import java.util.List;

import com.google.common.collect.Lists;

import de.tudarmstadt.ukp.muip.fibqdojo.Sentence;
import de.tudarmstadt.ukp.muip.fibqdojo.SentenceToken;
import de.tudarmstadt.ukp.muip.fibqdojo.Token;
import de.tudarmstadt.ukp.muip.fibqdojo.interfaces.IFIBQSentenceToken;
import de.tudarmstadt.ukp.muip.fibqdojo.interfaces.IStructureVisitor;
import de.tudarmstadt.ukp.muip.fibqdojo.japanese.JapaneseToken;
import de.tudarmstadt.ukp.muip.fibqdojo.util.japanese.KanaConverter;

/**
 * This class assembles a processed sentence as a linked list of string values.
 * If a token in the sentence was marked for being replaced with a blank the
 * respective token in the list comprises of '______'
 * 
 * @author Tobias Horsmann
 */
public class AssembleQuestionVisitor implements IStructureVisitor {

    private final String blank = "_______";
    private List<String> text = Lists.newArrayList();

    public List<String> getBlankedSentence() {
        return text;
    }

    @Override
    public void visit(JapaneseToken token) {
        if (token.isMarkedSimplified()) {
            String stringRep = makeToString(token.getForms());
            text.add(stringRep + "(" + translateKatakana2Hiragana(makeToString(token.getKana())) + ")");
        } else {
            text.add(makeToString(token.getForms()));
        }
    }

    private String translateKatakana2Hiragana(String katakanaWord) {
        String output = "";

        for (int i = 0; i < katakanaWord.length(); i++) {
            output += KanaConverter.katakana2hiragana("" + katakanaWord.charAt(i));
        }

        return output;
    }

    private String makeToString(String[] strings) {
        String returnVal = "";
        for (String form : strings) {
            returnVal += form;
        }
        return returnVal;
    }

    @Override
    public void visit(Token token) {
        text.add(token.getForm());
    }

    @Override
    public void visit(SentenceToken token) {
        if (token.isMarkedBlank()) {
            text.add(blank);
        } else {
            token.getToken().accept(this);
        }
    }

    @Override
    public void visit(Sentence sentence) {
        for (IFIBQSentenceToken token : sentence.getSentenceTokens()) {
            token.accept(this);
        }
    }

}
